﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Kocic.RepositoryManagement
{
    /// <summary>
    /// Base class for manager classes. Implements IDisposable since any data store connections are unmanaged and therefore will need to be disposed appropriately.
    /// </summary>
    /// <typeparam name="TDataSource">The type of the data source that will be used by the data managers.</typeparam>
    public abstract class DataSourceManager<TDataSource> : IDisposable
        where TDataSource : class
    {
        private Boolean _disposed = false;
        private Boolean _disposeDataSource;
        private TDataSource _dataSource;

        /// <summary>
        /// Creates an instance of the DataSourceManager class.
        /// </summary>
        protected DataSourceManager()
        {

        }

        #region IDisposable Members

        /// <summary>
        /// Disposes resources used by the manager.
        /// </summary>
        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        #endregion

        /// <summary>
        /// Disposes unmanaged resources used by the manager.
        /// </summary>
        /// <param name="disposing">Value indicating if to invoke the method from the IDisposable.Dispose implementation or from the finalizer.</param>
        protected virtual void Dispose(Boolean disposing)
        {
            if (disposing)
            {
                this._disposed = true;
            }
        }

        /// <summary>
        /// Gets or sets the object disposed state.
        /// </summary>
        protected Boolean IsDisposed
        {
            get { return this._disposed; }
            set { this._disposed = value; }
        }


        /// <summary>
        /// Gets the data source instance.
        /// </summary>
        protected TDataSource DataSource
        {
            get
            {
                return this._dataSource as TDataSource;
            }
            set
            {
                this._dataSource = value;
            }
        }

        /// <summary>
        /// Gets or sets whether or not to dispose the data source.
        /// </summary>
        protected Boolean DisposeDataSource
        {
            get { return this._disposeDataSource; }
            set { this._disposeDataSource = value; }
        }
    }
}